package com.tom.architect02.p4.day37.retrofit;


import okhttp3.HttpUrl;
import okhttp3.Request;

/**
 * Created by hcDarren on 2017/12/17.
 */
public class RequestBuilder {
    ParameterHandler<Object>[] parameterHandlers; // 参数： userName
    Object[] args; // 名字： Darren
    HttpUrl.Builder httpUrl; // okHttp自带的。

    public RequestBuilder(String baseUrl, String relativeUrl, String httpMethod,
                          ParameterHandler<?>[] parameterHandlers, Object[] args) {
        this.parameterHandlers = (ParameterHandler<Object>[]) parameterHandlers;
        this.args = args;
        this.httpUrl = HttpUrl.parse(baseUrl + relativeUrl).newBuilder();
    }

    public Request build() {
        int count = args.length;
        for (int i = 0; i < count; i++) { // 遍历参数
            // userName = Darren
            parameterHandlers[i].apply(this, args[i]); // 处理参数
        }
        // .... POST 等等
        Request request = new Request.Builder().url(httpUrl.build()).build();
        return request;
    }

    // 拼装键值对。
    public void addQueryName(String key, String value) {
        // userName = Darren&password = 940223
        httpUrl.addQueryParameter(key, value);
    }
    // 其他
}
